layui.config({
  base: "js/"
}).use(['form', 'layer', 'jquery', 'laypage'], function () {
  var form = layui.form(),
    layer = parent.layer === undefined ? layui.layer : parent.layer,
    laypage = layui.laypage,
    $ = layui.jquery;

  //加载页面数据
  var newsData = '';
  $.get("/api/course/all", function (data) {
    console.log(data);
    render(eval(data));
  });

  //查询
  $(".search_btn").click(function () {
    var newArray = [];
    if ($(".search_input").val() !== '') {
      var index = layer.msg('查询中，请稍候', {icon: 16, time: 3000, shade: 0.8});
      $.ajax({
        url: "/api/course/search?keywords=" + $(".search_input").val(),
        type: "get",
        dataType: "json",
        success: function (data) {
          layer.close(index);
          render(eval(data));
        }
      });
    } else {
      $.get("/api/course/all", function (data) {
        render(eval(data));
      });
    }
  })

  //添加文章
  $(window).one("resize", function () {
    $(".add_btn").click(function () {
      var index = layui.layer.open({
        title: "添加课程",
        type: 2,
        content: "add.html",
        area: ['60%', '90%'],
        success: function (layero, index) {
          setTimeout(function () {
            layui.layer.tips('点击此处返回课程列表', '.layui-layer-setwin .layui-layer-close', {
              tips: 3
            });
          }, 500)
        }
      });
      // layui.layer.full(index);
    })
  }).resize();

  //操作
  $("body").on("click", ".edit", function () {  //编辑
    var id = $(this).attr("data-id");
    console.log("data-id", id);
    $.get("/api/course", {Cos_id: id}, function (data) {
      //存储对象
      console.log(data);
      sessionStorage.setItem("course", data);
      var index = layui.layer.open({
        title: "修改课程",
        type: 2,
        content: "edit.html",
        area: ['60%', '90%'],
        success: function (layero, index) {
          setTimeout(function () {
            layui.layer.tips('点击此处返回课程列表', '.layui-layer-setwin .layui-layer-close', {
              tips: 3
            });
          }, 500)
        }
      });
      // layui.layer.full(index);
    });
  });

  $("body").on("click", ".del", function () {  //删除
    var id = $(this).attr("data-id");
    layer.confirm('确定删除此课程？', {icon: 3, title: '提示信息'}, function (index) {
      $.ajax({
        url: "/api/course",
        type: "DELETE",
        data: {Cos_id: id},
        dataType: "json",
        success: function (data) {
          layer.msg("删除成功！");
          window.location.reload();
        },
        error: function (data) {
          layer.warn("删除失败!");
        }
      });
      layer.close(index);
    });
  });

  function render(that) {
    //渲染数据
    function renderDate(data, curr) {
      var dataHtml = '';
      if (!that) {
        currData = newsData.concat().splice(curr * nums - nums, nums);
      } else {
        currData = that.concat().splice(curr * nums - nums, nums);
      }
      if (currData.length !== 0) {
        for (var i = 0; i < currData.length; i++) {
          dataHtml += '<tr>'
            + '<td align="left">' + currData[i].Cos_id + '</td>'
            + '<td>' + currData[i].Cos_name + '</td>'
            + '<td>' + currData[i].Cos_credit + '</td>'
            + '<td>' + currData[i].Cos_teacher + '</td>'
            + '<td>' + currData[i].Cos_college + '</td>'
            + '<td>' + currData[i].Cos_maxNum + '</td>'
            + '<td>' + currData[i].Cos_num + '</td>'
            + '<td>'
            + '<a class="layui-btn layui-btn-mini edit" data-id="' + currData[i].Cos_id + '"><i class="iconfont icon-edit"></i> 编辑</a>'
            + '<a class="layui-btn layui-btn-danger layui-btn-mini del" data-id="' + currData[i].Cos_id + '"><i class="layui-icon">&#xe640;</i> 删除</a>'
            + '</td>'
            + '</tr>';
        }
      } else {
        dataHtml = '<tr><td colspan="8">暂无数据</td></tr>';
      }
      return dataHtml;
    }

    //分页
    var nums = 10; //每页出现的数据量
    if (that) {
      newsData = that;
    }
    laypage({
      cont: "page",
      pages: Math.ceil(newsData.length / nums),
      jump: function (obj) {
        $(".content").html(renderDate(newsData, obj.curr));
        form.render();
      }
    })
  }
})
